// Author: wsfuyibing <682805@qq.com>
// Date: 2025-05-23

package get

import (
	"context"
	"gitee.com/go-libs/result"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/errs"
	"server/app/http/models"
	"server/app/http/services"
	"server/app/lib"
)

// Logic
// 加载会员设置.
type Logic struct {
	admin  *models.ArcadeAdmins
	arcade *models.Arcades

	response *Response
}

// New
// 创建逻辑实例.
func New() logics.Logical {
	return &Logic{
		response: &Response{},
	}
}

// Before
// 前置校验.
func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	o.arcade, o.admin = lib.Arcade.GetMaster().GetArcade(i)
	return
}

// Run
// 执行逻辑.
func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err     error
		has     bool
		setting *models.ArcadeSetting
		res     = result.New()
	)

	// 1. 读取设置.
	if setting, has, err = services.NewArcadeSettingService().Dao.GetById(ctx, o.arcade.Id); err != nil {
		return res.WithError(errs.ErrDatabaseQuery)
	}

	// 2. 无效设置.
	if !has {
		return res.WithError(errs.ErrArcadeSettingNotFound)
	}

	// 3. 设置结果.
	return res.With(o.response.With(setting))
}
